define_extension :deployment_notification, :none do
  task :notify_deployment_success do
  end
  
  task :notify_deployment_failure do
  end
  
  task :notify_deployment_success_summary do
  end
  
  task :notify_deployment_failure_summary do
  end
end

define_extension :task_notification, :none do
  task :notify_task_success do
  end
  
  task :notify_task_failure do
  end
  
  task :notify_task_success_summary do
  end
  
  task :notify_task_failure_summary do
  end  
end

mc.initializers << lambda { |name,conf|
  conf.instance_eval do
    on :deployment_succeeded do
      notify_deployment_success
    end

    on :deployment_failed do
      set :error, $!
      notify_deployment_failure
    end

    on :task_succeeded do
      set :task_name, outer_configuration.fetch(:task)
      notify_task_success
    end

    on :task_failed do
      set :task_name, outer_configuration.fetch(:task)
      set :error, $!
      notify_task_failure
    end
  end
}

mc.initializers << lambda { |name,conf|
  conf.use(:none).for(:deployment_notification)
  conf.use(:none).for(:task_notification)
}

# stub configuration to configure
define_cfg :summary_notifications do
  use(:none).for(:deployment_notification)
  use(:none).for(:task_notification)
end
set :summary_notifications, mc.find_configuration(:summary_notifications)
mc.initializers << lambda { |name,conf|
  conf.set :summary_notifications, conf.outer_configuration.summary_notifications  
}

on :deployment_succeeded do
  summary_notifications.notify_deployment_success_summary
end

on :deployment_failed do
  summary_notifications.set :deployment_error, $!
  summary_notifications.notify_deployment_failure_summary
end

on :task_succeeded do
  summary_notifications.set :task_name, fetch(:task)
  summary_notifications.notify_task_success_summary
end

on :task_failed do
  summary_notifications.set :task_name, fetch(:task)
  summary_notifications.set :task_execution_error, $!
  summary_notifications.notify_task_failure_summary
end
